package ynu.edu.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ynu.edu.dto.BusinessFoodDTO;
import ynu.edu.dto.FoodDTO;
import ynu.edu.service.FoodService;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 食品控制器
 */
@RestController
@RequestMapping("/food")
@RequiredArgsConstructor
@Slf4j
public class FoodController {
    
    private final FoodService foodService;
    
    /**
     * 根据商家ID获取食品列表
     *
     * @param businessId 商家ID
     * @return 食品列表
     */
    @GetMapping("/getByBusinessId/{businessId}")
    public ResponseEntity<List<FoodDTO>> getFoodsByBusinessId(@PathVariable Integer businessId) {
        log.info("根据商家ID获取食品列表，businessId: {}", businessId);
        List<FoodDTO> foods = foodService.getFoodsByBusinessId(businessId);
        log.info("获取到{}个食品", foods.size());
        return ResponseEntity.ok(foods);
    }
    
    /**
     * 根据商家ID和分类ID获取食品列表
     *
     * @param businessId 商家ID
     * @param categoryId 分类ID
     * @return 食品列表
     */
    @GetMapping("/getByCategory")
    public ResponseEntity<List<FoodDTO>> getFoodsByCategory(@RequestParam Integer businessId, @RequestParam Integer categoryId) {
        log.info("根据商家ID和分类ID获取食品列表，businessId: {}, categoryId: {}", businessId, categoryId);
        List<FoodDTO> foods = foodService.getFoodsByBusinessIdAndCategoryId(businessId, categoryId);
        log.info("获取到{}个食品", foods.size());
        return ResponseEntity.ok(foods);
    }
    
    /**
     * 根据ID获取食品
     *
     * @param foodId 食品ID
     * @return 食品信息
     */
    @GetMapping("/getById/{foodId}")
    public ResponseEntity<FoodDTO> getFoodById(@PathVariable Integer foodId) {
        log.info("根据ID获取食品，foodId: {}", foodId);
        FoodDTO food = foodService.getFoodById(foodId);
        log.info("获取到食品: {}", food.getFoodName());
        return ResponseEntity.ok(food);
    }
    
    /**
     * 根据商家ID获取食品列表和商家信息
     *
     * @param businessId 商家ID
     * @return 商家和食品信息
     */
    @GetMapping("/getWithMerchant/{businessId}")
    public ResponseEntity<BusinessFoodDTO> getFoodsWithMerchant(@PathVariable Integer businessId) {
        log.info("根据商家ID获取食品列表和商家信息，businessId: {}", businessId);
        BusinessFoodDTO businessFood = foodService.getFoodsWithMerchant(businessId);
        log.info("获取到商家: {}, 食品分类: {}, 食品: {}",
                businessFood.getMerchant().getBusinessName(),
                businessFood.getCategories().size(),
                businessFood.getFoods().size());
        return ResponseEntity.ok(businessFood);
    }
    
    /**
     * 添加食品
     *
     * @param foodDTO 食品信息
     * @return 添加后的食品信息
     */
    @PostMapping("/add")
    public ResponseEntity<FoodDTO> addFood(@Valid @RequestBody FoodDTO foodDTO) {
        log.info("添加食品: {}, 所属商家ID: {}", foodDTO.getFoodName(), foodDTO.getBusinessId());
        FoodDTO addedFood = foodService.addFood(foodDTO);
        log.info("食品添加成功，ID: {}", addedFood.getFoodId());
        return new ResponseEntity<>(addedFood, HttpStatus.CREATED);
    }
    
    /**
     * 更新食品
     *
     * @param foodDTO 食品信息
     * @return 更新后的食品信息
     */
    @PutMapping("/update")
    public ResponseEntity<FoodDTO> updateFood(@Valid @RequestBody FoodDTO foodDTO) {
        log.info("更新食品，foodId: {}, 食品名: {}", foodDTO.getFoodId(), foodDTO.getFoodName());
        FoodDTO updatedFood = foodService.updateFood(foodDTO);
        log.info("食品更新成功");
        return ResponseEntity.ok(updatedFood);
    }
    
    /**
     * 删除食品
     *
     * @param foodId 食品ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{foodId}")
    public ResponseEntity<Void> deleteFood(@PathVariable Integer foodId) {
        log.info("删除食品，foodId: {}", foodId);
        foodService.deleteFood(foodId);
        log.info("食品删除成功");
        return ResponseEntity.noContent().build();
    }
    
    /**
     * 搜索食品
     *
     * @param keyword 关键词
     * @return 食品列表
     */
    @GetMapping("/search")
    public ResponseEntity<List<FoodDTO>> searchFoods(@RequestParam String keyword) {
        log.info("搜索食品，关键词: {}", keyword);
        List<FoodDTO> foods = foodService.searchFoods(keyword);
        log.info("搜索到{}个食品", foods.size());
        return ResponseEntity.ok(foods);
    }
} 